package nccloud.grid.ct.contructionpermit;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.microsoft.sqlserver.jdbc.StringUtils;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nccloud.framework.web.container.SessionContext;
import nccloud.framework.web.processor.refgrid.RefQueryInfo;
import nccloud.framework.web.ui.meta.RefMeta;
import nccloud.web.refer.DefaultGridRefAction;

public class DefaultPuYSKMFGrid extends DefaultGridRefAction {

	public DefaultPuYSKMFGrid() {
		// TODO Auto-generated constructor stub
		super();
	}

	@Override
	public RefMeta getRefMeta(RefQueryInfo arg0) {
		// TODO Auto-generated method stub

		RefMeta refmeta = new RefMeta();
		// 参照
		String org = arg0.getQueryCondition().get("v_org");

		String org2 = arg0.getQueryCondition().get("v_ORG");
		if (org2 != null) {
			org = org2;
		}
		
//		if(org == null) {
//			String org3 = arg0.getQueryCondition().get("pk_org");
//			org = org3;
//		}
		
		
		// 项目
		String xm = arg0.getQueryCondition().get("xd");
		String type = arg0.getQueryCondition().get("type");
		if (null == type) {
			refmeta.setCodeField("code");// code
			refmeta.setNameField("name");// 名称
			refmeta.setPkField("pk_accasoa");// id
			refmeta.setTableName(getExtableSql(org));// 表名

			// 查询的额外字段
			// 合同号，合同名称，行号，物料编码，物料名称
			refmeta.setExtraFields(new String[] { "org", "name", "code", "kmName", "pk_accasoa", "aaname", "bbname",
					"ccname", "aacode", "bbcode", "cccode" });

			// 参照是否需要多语
			refmeta.setMutilLangNameRef(false);
		} else {
			List<String> pk_s = new ArrayList<>();
			try {
				String pj = null;
				if (xm != null) {
					String bd_project = "select pk_eps from bd_project  where pk_project='" + xm + "'";
					pj = (String) new BaseDAO().executeQuery(bd_project, new ColumnProcessor());
				}
				StringBuffer query = new StringBuffer();
				query.append("select def1 from ct_kjkm_new_smaster where dr=0     ");
				String appCodeNCC = SessionContext.getInstance().getAppcode();
				query.append(
						"and def3 in (select pk_defdoc from bd_defdoc where pk_defdoclist in (select pk_defdoclist from bd_defdoclist  where code='YY101')  and code = '"
								+ appCodeNCC + "')");

				if (org != null) {
					query.append("and def4='" + org + "'");
				}
				if (pj != null) {
					query.append("and def2='" + pj + "'");
				} else {
					query.append("and (def2 is null or def2 = '~') ");
				}
				pk_s = (List<String>) new BaseDAO().executeQuery(query.toString(), new ColumnListProcessor());

			} catch (DAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			refmeta.setCodeField("code");// code
			refmeta.setNameField("name");// 名称
			refmeta.setPkField("pk_accasoa");// id
			refmeta.setTableName(getExtableSqlcheck(pk_s, org));// 表名

			// 查询的额外字段
			// 合同号，合同名称，行号，物料编码，物料名称
			refmeta.setExtraFields(new String[] { "org", "name", "code", "kmName", "pk_accasoa", "aaname", "bbname",
					"ccname", "aacode", "bbcode", "cccode" });
			// 参照是否需要多语
			refmeta.setMutilLangNameRef(false);
		}
		return refmeta;
	}

	private String getExtableSql(String org) {
		StringBuffer query = new StringBuffer();
		query.append(" (");
		query.append(
				"SELECT  distinct oo.name AS org,   ba.name AS name ,substring_index(ba.dispname2, '\\', 1)   AS  code  , bat.name AS kmName  , ba.pk_accasoa AS pk_accasoa,ba.pk_accchart "
						+ " , (case when length(baacc.code) in (5,7,9) then left(baacc.code,3) when length(baacc.code) in (6,8,10,12) then left(baacc.code,4) else '' end ) cccode "
						+ " , (case when length(baacc.code) in (7,9) then left(baacc.code,5) when length(baacc.code) in (8,10,12) then left(baacc.code,6) else '' end ) bbcode "
						+ " , (case when length(baacc.code) in (9) then left(baacc.code,7) when length(baacc.code) in (10,12) then left(baacc.code,8) else '' end ) aacode"
						+ " , (case when length(baacc.code) in (6,8,10,12) then replace(substring_index(ba.dispname,'\\',2),substring_index(ba.dispname,'\\',1)||'\\','') else '' end ) ccname"
						+ " , (case when length(baacc.code) in (8,10,12) then replace(substring_index(ba.dispname,'\\',3),substring_index(ba.dispname,'\\',2)||'\\','') else '' end ) bbname"
						+ " , (case when length(baacc.code) in (10,12) then replace(substring_index(ba.dispname,'\\',4),substring_index(ba.dispname,'\\',3)||'\\','') else '' end ) aaname" );
		
		query.append(" FROM org_accountingbook  oacb  \n");
		query.append("LEFT JOIN  org_orgs oo ON  OACB.pk_relorg=oo.pk_org  \n");
		query.append("LEFT join bd_accchart bat  ON oacb.pk_curraccchart =bat.pk_accchart  \n");
		query.append("LEFT JOIN bd_accasoa ba ON ba.pk_accchart =bat.pk_accchart  \n");
		query.append("LEFT JOIN bd_account baacc on baacc.pk_account = ba.pk_account ");
		query.append("WHERE  ba.dispname LIKE '%\\%' AND  ba.dr=0 ");
		if (org != null) {
			query.append("AND oo.pk_org='" + org + "'");
		}
		query.append(" )");
		return query.toString();
	}

	private String getExtableSqlcheck(List<String> pk_s, String org) {
		pk_s = (List<String>) pk_s.stream().distinct().collect(Collectors.toList());
		StringBuffer query = new StringBuffer();
		query.append(" (");
		query.append("SELECT  distinct oo.name AS org,   ba.name AS name ,baacc.code, bat.name AS kmName  , ba.pk_accasoa AS pk_accasoa,ba.pk_accchart "
						+ " , (case when length(baacc.code) in (5,7,9) then left(baacc.code,3) when length(baacc.code) in (6,8,10,12) then left(baacc.code,4) else '' end ) cccode "
						+ " , (case when length(baacc.code) in (7,9) then left(baacc.code,5) when length(baacc.code) in (8,10,12) then left(baacc.code,6) else '' end ) bbcode "
						+ " , (case when length(baacc.code) in (9) then left(baacc.code,7) when length(baacc.code) in (10,12) then left(baacc.code,8) else '' end ) aacode"
						+ " , (case when length(baacc.code) in (6,8,10,12) then replace(substring_index(ba.dispname,'\\',2),substring_index(ba.dispname,'\\',1)||'\\','') else '' end ) ccname"
						+ " , (case when length(baacc.code) in (8,10,12) then replace(substring_index(ba.dispname,'\\',3),substring_index(ba.dispname,'\\',2)||'\\','') else '' end ) bbname"
						+ " , (case when length(baacc.code) in (10,12) then replace(substring_index(ba.dispname,'\\',4),substring_index(ba.dispname,'\\',3)||'\\','') else '' end ) aaname" );
		query.append(" FROM org_accountingbook  oacb  \n");
		query.append("LEFT JOIN  org_orgs oo ON  OACB.pk_relorg=oo.pk_org  \n");
		query.append("LEFT join bd_accchart bat  ON oacb.pk_curraccchart =bat.pk_accchart  \n");
		query.append("LEFT JOIN bd_accasoa ba ON ba.pk_accchart =bat.pk_accchart  \n");
		query.append("LEFT JOIN bd_account baacc on baacc.pk_account = ba.pk_account ");
		query.append("WHERE  ba.dispname LIKE '%\\%' AND  ba.dr=0 ");
		query.append("and ba.endflag = 'Y' ");
		if (!pk_s.isEmpty()) {
			List<String> allL = new ArrayList<>();
			pk_s.stream().forEach(s -> {
				s = "'" + s + "'";
				allL.add(s);
			});

			String listAll = allL.stream().collect(Collectors.joining(","));
			query.append("and  ba.pk_accasoa in (" + listAll + "" + ")");
		} 
		else {
			query.append(" and 1=0 ");
		}
		if (org != null) {
			query.append("AND oo.pk_org='" + org + "'");
		}
		query.append(" ) ");
		return query.toString();
	}
}
